//
//  ZuantiDetailViewController.m
//  Travel
//
//  Created by 吴品泽 on 15/4/9.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "ZuantiDetailViewController.h"
#import <AFHTTPRequestOperationManager.h>
#import <UIImageView+WebCache.h>

#import "ZuantiModel.h"
#import "ZuantiModelOne.h"

#import "DetailCunImageCell.h"
#import "DetailTableViewCell.h"
#import "TitleAndDesCell.h"
#import "DetalImagAndLabelCell.h"

@interface ZuantiDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain)UITableView *tableView;


@property (nonatomic,strong)UIImageView *traImageView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,retain)UILabel *dettitleLabel;
@property (nonatomic,retain)UILabel *horeLabel;
@property (nonatomic,retain)UIView *backView;

@property (nonatomic,strong)ZuantiModel *Model;
@property (nonatomic,strong)NSMutableArray *arr;

@end

@implementation ZuantiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self requestData];
}

- (void)createHeadView
{
    UIImageView *headView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT * 0.3)];
    NSURL *urlimage = [NSURL URLWithString:self.Model.image_url];
#pragma 占位图没加
    headView.userInteractionEnabled = YES;
    [headView sd_setImageWithURL:urlimage placeholderImage:nil];
    headView.backgroundColor = [UIColor yellowColor];
    self.horeLabel = [[UILabel alloc]init];
    self.titleLabel = [[UILabel alloc]init];
    self.dettitleLabel = [[UILabel alloc]init];
    // self.traImageView.backgroundColor = [UIColor blackColor];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:22];
    self.titleLabel.frame = CGRectMake(0, headView.frame.size.height / 3, headView.frame.size.width ,40);
    self.titleLabel.textColor = [UIColor whiteColor];
    //self.titleLabel.backgroundColor = [UIColor yellowColor];
    self.horeLabel.frame = headView.frame;
    self.horeLabel.backgroundColor = [UIColor blackColor];
    
    self.dettitleLabel.frame = CGRectMake(0 ,headView.frame.size.height / 3 + 40 , headView.frame.size.width  , 40);
    self.dettitleLabel.textColor = [UIColor whiteColor];
    self.dettitleLabel.font = [UIFont systemFontOfSize:16];
    
    self.titleLabel.text = self.Model.name;
    self.dettitleLabel.text = self.Model.title;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.dettitleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.horeLabel.alpha = 0.2;
    [headView addSubview:self.horeLabel];
    [headView addSubview:self.titleLabel];
    [headView addSubview:self.dettitleLabel];
    
    self.tableView.tableHeaderView = headView;
}

- (void)requestData
{
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/articles/%@.json?page=1",self.urlStr];
    
   // NSLog(@"#############%@",string);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:string parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.Model = [[ZuantiModel alloc]init];
        
        [self.Model setValuesForKeysWithDictionary:responseObject];
        
        [self.Model getZuantiModelOne];
        
       // NSLog(@"%@",self.Model.name);
       // NSLog(@"%@",self.Model.article_sections);
        
        
       //  0x7fb19e044110
        [self createHeadView];
       
        [self.tableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求出错了( ⊙ o ⊙ )！");
    }];
    
}




- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Model.article_sections.count - 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZuantiModelOne *modes = [self.Model.article_sections objectAtIndex:indexPath.row];
  //  NSLog(@")))))))))))))%@",modes);
    
//    static NSString *strr = @"fangfang";
//    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strr];
//    
//            if (cell == nil) {
//                cell = [[DetailTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strr];
//            }
//    
//    cell.Detaillabel.text = @"你好啊";
//    
//    return cell;
    //纯文字(不带标题)
    if ([modes.title isEqualToString:@""] && [modes.image_url isEqualToString:@""] && ![modes.Adescription isEqualToString:@""]) {
        
        static NSString *strr = @"fangfang";
        DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strr];
        
        if (cell == nil) {
            cell = [[DetailTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strr];
        }
        
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        CGRect rect = [modes.Adescription boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        cell.Detaillabel.frame = CGRectMake(10, 10, self.view.frame.size.width - 20,  rect.size.height + 10);
        
        cell.Detaillabel.font = [UIFont systemFontOfSize:15];
        cell.Detaillabel.text =modes.Adescription;
        
        return cell;
      //纯图片
    }else if ([modes.title isEqualToString:@""] && ![modes.image_url isEqualToString:@""] &&   [modes.Adescription isEqualToString:@""]){
        
        
        static NSString *str = @"hehe2";
        DetailCunImageCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[DetailCunImageCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSNumber *wide =  modes.image_width;
        NSNumber *height = modes.image_height;
        
        NSInteger wi = [wide integerValue];
        NSInteger hi = [height integerValue];
        
        CGFloat  EndHight = hi * self.view.frame.size.width  * 1.0 / wi;
        
        cell.MyimageView.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, EndHight );
      
        
        NSURL *url = [NSURL URLWithString:modes.image_url];
#pragma 占位图
        [cell.MyimageView sd_setImageWithURL:url placeholderImage:nil];
        
        return cell;
        //文字加图片
    }else if ([modes.title isEqualToString:@""] && ![modes.image_url isEqualToString:@""] && ![modes.Adescription isEqualToString:@""]){
        
        static NSString *strs = @"hehe1";
        DetalImagAndLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
        if (cell == nil) {
            cell = [[DetalImagAndLabelCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strs];
        }
        
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        
        CGRect rect = [modes.Adescription boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        NSNumber *wide =  modes.image_width;
        NSNumber *height = modes.image_height;

        
        NSInteger wi = [wide integerValue];
        NSInteger hi = [height integerValue];
        
        CGFloat  EndHight = hi * self.view.frame.size.width  * 1.0 / wi;
      //   NSLog(@"222222222222222222%@",modes.image_url);
        
       //   NSLog(@"<<<<<<<区%ld,>>>>>>>>>cell:%ld",indexPath.section,indexPath.row);
        cell.MYimageView.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, EndHight - 20);
        cell.deLabel.frame = CGRectMake(10,  EndHight - 10 , self.view.frame.size.width - 20, rect.size.height + 20);
        
        //  NSLog(@"%ld---%g   %g %g %g",indexPath.row,cell.deLabel.frame.size.width,cell.deLabel.frame.origin.x,cell.contentView.frame.size.width,cell.frame.size.width);
       
        NSURL *url = [NSURL URLWithString:modes.image_url];
#pragma 占位图
        [cell.MYimageView sd_setImageWithURL:url placeholderImage:nil];
        
        cell.deLabel.text = modes.Adescription;
        cell.deLabel.font = [UIFont systemFontOfSize:15];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
   
        
        ////头部name + 文字
    }else  if (![modes.title isEqualToString:@""] && [modes.image_url isEqualToString:@""] && ![modes.Adescription isEqualToString:@""]){
        
        
        static NSString *str = @"hehe3";
        TitleAndDesCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[TitleAndDesCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        
        CGRect rect = [modes.Adescription boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect1 = [modes.title boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
        
        
        cell.titleLabel.frame = CGRectMake(10, 10, WIDTH - 20, rect1.size.height);
        cell.descriLabe.frame = CGRectMake(10,  rect1.size.height + 10, WIDTH - 20, rect.size.height);
        cell.titleLabel.text = modes.title;
        cell.descriLabe.text = modes.Adescription;
        
        return cell;
        
    }
    static NSString *str = @"hehe4";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }

    return cell;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZuantiModelOne *modes = [self.Model.article_sections objectAtIndex:indexPath.row];
    
    //纯文字
    if ([modes.title isEqualToString:@""] && [modes.image_url isEqualToString:@""] && ![modes.Adescription isEqualToString:@""]) {
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        CGRect rect = [modes.Adescription boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return rect.size.height + 30;
    }
    
    //文字加图片
    if ([modes.title isEqualToString:@""] && ![modes.image_url isEqualToString:@""] && ![modes.Adescription isEqualToString:@""]) {
        
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        
        CGRect rect = [modes.Adescription boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        NSNumber *wide = modes.image_width;
        NSNumber *height = modes.image_height;
        
        NSInteger wi = [wide integerValue];
        NSInteger hi = [height integerValue];
        
        CGFloat  EndHight = hi * self.view.frame.size.width  * 1.0 / wi;
        
        //  NSLog(@"1:%.2f", rect.size.height + EndHight + 20);
        return rect.size.height + EndHight + 20;
    }
    
    //头部name + 文字
    
    if (![modes.title isEqualToString:@""] && [modes.image_url isEqualToString:@""] && ![modes.Adescription isEqualToString:@""]) {
        
        NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
        
        CGRect rect = [modes.Adescription boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
         NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        CGRect rect1 = [modes.title boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
        
        return rect.size.height + rect1.size.height + 30;
        
    }
    
    //纯图片的时候
    
    if ([modes.title isEqualToString:@""] && ![modes.image_url isEqualToString:@""] && [modes.Adescription isEqualToString:@""]) {
        
        
        NSNumber *wide = modes.image_width;
        NSNumber *height = modes.image_height;
        
        NSInteger wi = [wide integerValue];
        NSInteger hi = [height integerValue];
        
        CGFloat  EndHight = hi * self.view.frame.size.width  * 1.0 / wi;
        
        //  NSLog(@"1:%.2f", rect.size.height + EndHight + 20);
        return  EndHight + 20;
        
    }
    
    
    if ([modes.title isEqualToString:@""] && [modes.image_url isEqualToString:@""] && [modes.Adescription isEqualToString:@""]) {
        return 0;
    }
    
    return 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
